﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MenuGame2DXNA
{
    public class Dialog : VisibleGameEntity
    {
       
        private Game game;      
        int Time ;
       
        MyDrawWithAlpha bodyDialog;
        GroupLayout headDialog;
        VisibleEntityManager veManager;
        int Width;
        int Height;
        private DialogState state;
        int TotalTime;
        TextEntity tilte;

        public DialogState State
        {
            get { return state; }
            set { state = value; }
        }
        int Timeout;
        public   Dialog (Game _game,int width, int height,VisibleGameEntity obj,string tit="",int _timeOut = 0,int _leftAlign = -1)
        {
            InitDialog(_game, width, height, _timeOut,tit, _leftAlign);
            if (obj != null)
                veManager.AddObject(obj);
        }

        public Dialog(Game _game, List<VisibleGameEntity> objs, int width = 600, int height =400,string tit="", int _timeOut = 0, int _leftAlign = -1)
        {
            InitDialog(_game, width, height, _timeOut,tit, _leftAlign);
            foreach (VisibleGameEntity obj in objs)
            {
                if (obj != null)
                    veManager.AddObject(obj);
            }
            
        
        }

            private void InitDialog(Game _game, int width, int height, int _timeOut,string tit, int _leftAlign)
            {
                game = _game;
                Height = height;
                Width = width;
                Timeout = _timeOut;
                veManager = new VisibleEntityManager();

                int LeftAlign = 0;
                if (_leftAlign == -1)
                    LeftAlign = (800 - width) / 2;
                else
                    LeftAlign = _leftAlign;
                headDialog = new GroupLayout(game.Content, "LayoutDialogHelp.xml", "");
                headDialog["Topbar"].Size = new Vector2(width, headDialog["Topbar"].Size.Y);
                headDialog["Topbar"].PositionStart = new Vector2(LeftAlign, headDialog["Topbar"].PositionStart.Y);
                headDialog["Xichtrai"].PositionStart = new Vector2(LeftAlign, headDialog["Xichtrai"].PositionStart.Y);
                headDialog["Xichphai"].PositionStart = new Vector2(Width + LeftAlign - 60, headDialog["Xichphai"].PositionStart.Y);

                Texture2D rect = new Texture2D(game.GraphicsDevice, width, height - 30);

                Color[] data = new Color[width * (height - 30)];
                for (int i = 0; i < data.Length; ++i) data[i] = Color.Black;
                rect.SetData(data);

                Vector2 coor = new Vector2(LeftAlign, 100);
                bodyDialog = new MyDrawWithAlpha(rect, coor);
                tilte = new TextEntity(game.Content, tit);
                tilte.ColorText = Color.Yellow;
                tilte.PositionStart = new Vector2(LeftAlign + width / 3, 74);
                tilte.Align = TextEntity.Alignment.Center;
                veManager.AddObject(bodyDialog);
                veManager.AddObject(headDialog);
                veManager.AddObject(tilte);
                veManager.TopLeft = new Vector2(0, -Height - 120);
                Time = 0;
                State = DialogState.NONE;
            }
        public void Appear(int _totaltime)
        {
            State = DialogState.APPEAR;
           
            TotalTime = _totaltime;
            Time = 0;
        }
        public void DisAppear(int _totaltime)
        {
            State = DialogState.DISAPPEAR;
            TotalTime = _totaltime;
            Time = 0;
        }
        public override void Update(GameTime gameTime, Color color)
        {
            if (State == DialogState.APPEAR)
            {
                if(Time == 10)
                    SoundManager.Play("boom");
                UpdatePositon(0, TotalTime - 24, veManager, new Vector2(0, 10));

                UpdatePositon(TotalTime - 23, TotalTime -14, veManager, new Vector2(0, -10));

                UpdatePositon(TotalTime - 13, TotalTime - 9, veManager, new Vector2(0, 5));

                UpdatePositon(TotalTime - 8, TotalTime - 4, veManager, new Vector2(0, -5));

                UpdatePositon(TotalTime - 3, TotalTime, veManager, new Vector2(0, 0));

                if (Time == TotalTime)
                {
                    State = DialogState.ENDAPPEAR;
                    
                }
            }
            if (Timeout !=0 && State == DialogState.ENDAPPEAR && Timeout / 16 == Time)
                DisAppear(30);
            if (State == DialogState.DISAPPEAR)
            {
                UpdatePositon(0, TotalTime, veManager, new Vector2(0, -Height - 120));
                if (Time == TotalTime)
                {
                    State = DialogState.ENDDISAPPEAR;
                    
                }
            }
            Time++;
            veManager.Update(gameTime, color);
        }


        public override void Draw(GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            veManager.Draw(gameTime, spriteBatch);
        }


        public void UpdatePositon(int TimeStart, int TimeEnd, VisibleGameEntity objectVisiable, Vector2 PositionEnd)
        {
            if ((TimeStart <= Time) && (TimeEnd > Time))
            {
                Vector2 PositionStart = objectVisiable.TopLeft;
                float X = PositionEnd.X - PositionStart.X;
                float Y = PositionEnd.Y - PositionStart.Y;

                float DistanceX = X / (TimeEnd - Time);

                float DistanceY = Y / (TimeEnd - Time);
                PositionStart = new Vector2(PositionStart.X + DistanceX, PositionStart.Y + DistanceY);
                objectVisiable.TopLeft = PositionStart;
            }

        }
        public override void UpdatePositon()
        {
            
        }

        public enum DialogState { APPEAR = 1, DISAPPEAR, ENDAPPEAR, ENDDISAPPEAR, NONE }
    }
    
}
